//
//  EJSManageAddressViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/13.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSManageAddressViewController.h"
#import "EJSEditAddressViewController.h"
#import "EJSAddressTableViewCell.h"

#import "EJSAddressListRequest.h"
#import "EJSDeleteAddressRequest.h"

@interface EJSManageAddressViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation EJSManageAddressViewController

+ (instancetype)viewController
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    return [storyBoard instantiateViewControllerWithIdentifier:@"EJSManageAddressViewController"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"地址列表";
    // Do any additional setup after loading the view from its nib.
    [self configTabelView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getAddressList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configTabelView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSAddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSAddressTableViewCell"];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    self.tableView.tableFooterView = [UIView new];
}

- (void)leftBarButtonAction:(id)sender{
    if(self.block){
        self.block(self.selectedAddressModel);
    }
    [super leftBarButtonAction:nil];
}


- (IBAction)addAddressAction:(UIButton *)sender
{
//    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];

   EJSEditAddressViewController *editAddressVC = [EJSEditAddressViewController viewController];
    [self.navigationController pushViewController:editAddressVC animated:YES];
}

- (void)getAddressList{
    [[EJSAddressListRequest new] asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [self.dataSource removeAllObjects];
        if(response.data){
            self.dataSource = [NSMutableArray arrayWithArray:response.data];
        }
        if(self.dataSource.count > 0){
            [self hideNoDataView];
        }else{
            [self showNoDataViewWithText:@"暂无收货地址"];
        }

        [self.tableView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

#pragma mark - Table View DataSource Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 92;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EJSAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSAddressTableViewCell" forIndexPath:indexPath];
    EJSAddressModel *addressModel = [self.dataSource objectAtIndex:indexPath.row];
    [cell setManageAddressType:self.manageAddressType addressModel:addressModel];
    if([addressModel.addressId isEqualToString:self.selectedAddressModel.addressId]){
        cell.selectButton.selected = YES;
        self.selectedAddressModel = addressModel;
    }else{
        cell.selectButton.selected = NO;
    }
    return cell;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FlyWeakSelf
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" 编辑 " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        tableView.editing = NO;
        EJSAddressModel *model = self.dataSource[indexPath.row];
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
        EJSEditAddressViewController *editAddressVC = [storyBoard instantiateViewControllerWithIdentifier:@"EJSEditAddressViewController"];
        editAddressVC.editAddressModel = model;
        [weakSelf.navigationController pushViewController:editAddressVC animated:YES];
    }];
    editAction.backgroundColor = UIColorFromRGB(0x3C4151);
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"  删除 " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        tableView.editing = NO;
        EJSAddressModel *model = self.dataSource[indexPath.row];
        EJSDeleteAddressRequest *req = [EJSDeleteAddressRequest new];
        req.addressId = model.addressId;
        [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
            [weakSelf getAddressList];
        } failHandler:^(BaseResponse *response) {
            [[HUDHelper sharedInstance] tipMessage:response.message];
        }];
        
    }];
    deleteAction.backgroundColor = UIColorFromRGB(0x3C4151);
    return @[deleteAction,editAction];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectedAddressModel = [self.dataSource objectAtIndex:indexPath.row];
    if(self.block){
        self.block(self.selectedAddressModel);
        [self leftBarButtonAction:nil];
    }
}


@end
